import axios from 'axios'
import qs from 'qs'
import * as  constant from '@/Constant/constant'

axios.defaults.timeout = 10000;            //响应时间
axios.defaults.baseURL =constant.BASE_URL;   //配置接口地址

// //POST传参序列化
axios.interceptors.request.use((config) => {
  // let token=constant.getCookie("token")
  // if(token){
  //   config.headers['X-Token']= token;
  //   // config.data = qs.stringify(config.data);
  // }
  return config;
},(error) =>{
  // _.toast("错误的传参", 'fail');
  return Promise.reject(error);
})

//返回状态判断(添加响应拦截器)
axios.interceptors.response.use((res) => {
  //对响应数据做些事
  if (!res.data.successful) {
    if(res.data){
      return res.data
    }else{
      return Promise.reject(res);
    }
  }
  return res.data.resultValue
}, (error) => {
  // alert('网络异常');
  if (error.response) {
    switch (error.response.status) {
      case 401:
        // constant.delCookie("user")
        // constant.delCookie("token")
        // this.$router.replace({
        //   path: 'login',
        //   query: {redirect: router.currentRoute.fullPath}
        // })
    }
  }
  return Promise.reject(error);
});

// 获取登录信息
function getUserInfo(name) {
  return axios.get('/user/user?name='+name)
}
function getUsers() {
  return axios.post('/user/getUsers')
}
//安全保障
function getAllInfo(){
  return axios.get('/security/find');
}
//在线预约
function submitBookInfo(data){
  return axios.post("/appointment/save",{"name":data.name,"email":data.email,"phone":data.phone,"title":data.title,"content":data.content});
}
//获取师资团队信息
function getAllTeacherInfo(){
  return axios.get("/team/find");
}
//获取单个教练信息
function getSingleTeacherInfo(id){
  return axios.get("/team/findById/"+id);
}
export default {
  getUserInfo,
  getUsers,
  getAllInfo,
  submitBookInfo,
  getAllTeacherInfo,
  getSingleTeacherInfo
}
